/*
 * @Author: river
 * @since: 2019-06-13 22:03:50
 * @lastTime: 2019-06-18 21:11:38
 * @LastAuthor: river
 */
/**
 * 接口
 */
interface LabelledValue {
    label: string,
    size: number,
    sex?: string
}

function printLabel(labelledObj: LabelledValue) {
    console.log(labelledObj);
}

let myObj = { size: 10, label: "Size 10 Object", sex: 'men' };
printLabel(myObj);


interface Point {
    readonly x: number;
}

let p1: Point = { x: 5 }
// error p1.x=6


let a: number[] = [1, 2, 3, 4]
let ro: ReadonlyArray<number> = a;

a[2] = 5

a.push(789)
a = ro as number[];
//ro[2] = 5
console.log(a);

interface SquareConfig {
    color?: string;
    width?: number;
    [propName: string]: any
}

function createSquare(config) {
    console.log(config);
}

let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig);
//函数类型
interface SearchFunc {
    (source: string, subString: string): boolean;
}

let mySearch: SearchFunc;

mySearch = function (source: string, subString: string) {
    return true;
}
mySearch('a', '1');
// 类类型
// 实现接口
// interface ClockInterface {
//     currentTime: Date;
//     setTime(d: Date);
//     new(h: number, m: number);
// }

// class Clock implements ClockInterface {
//     currentTime: Date;
//     setTime(d: Date) { }
//     constructor(h: number, m: number) { }
// }

interface ClockConstructor {
    new(hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
    tick();
}

function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
    return new ctor(hour, minute);
}

class DigitalClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("beep beep");
    }
}
class AnalogClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("tick tock");
    }
}

let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);
digital.tick();

//继承接口

interface Shape {
    color: string;
}

interface Square extends Shape {
    sidelength: number;
}

let square = <Square>{};

// square.a = 1


// 混合类型
interface Counter {

    interval: number;
    reset(): void;
}

function getCounter(): Counter {
    let counter = <Counter>{};
    counter.interval = 123;
    counter.reset = function () { };
    return counter;
}

let c = getCounter();
// c(10);
c.reset();
c.interval = 5.0;

// 接口继承类
class Control {
    private state: any;
}

interface SelectableControl extends Control {
    select(): void;
}

class Button extends Control implements SelectableControl {

    select() { }
}

console.log(new Button());
class TextBox extends Control {
    select() { }
}

// 错误：“Image”类型缺少“state”属性。
// class Image implements SelectableControl {
//     select() { }
// }

class Location1 {
    a = 1;
}